/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-prototype-builtins */
import "./index.less";
import { useTranslation } from "react-i18next";
import PatientInformation from "@/components/ViewComponents/PatientInformation/index";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { useRef } from "react";
import { useFetchHistoryData, History } from "@/components/ViewComponents/History/index";
import { TransferPatient } from "@/components/ViewComponents/TransferPatient";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { useDrawer } from "@/components/Drawer";
import { admissionsCreate, dischargesCreate, patientSynthesisList } from "@/api/SecondmentDictionary";
import { thIpmAdmissions } from "@/api/ImmigrationDictionary";
import { DICT_TYPE, getDictOptions } from "@/utils/dict";
import { useMessage } from "@/hooks/web/useMessage";
// diagnosisGetPat

const outpatient = () => {
	const { t } = useTranslation();
	const { createMessage } = useMessage();

	const btnList = ["save", "transfer"];
	const formRightTopFormSchema: FormSchema[] = [
		{
			label: t("kevin_ui.arrived_for_examination_at"),
			component: "DatePicker",
			field: "examStartDtime"
		},
		// {
		// 	label: t("kevin_ui.from"),
		// 	component: "Input",
		// 	field: "deathFrom",
		// 	colProps: { span: 8 }
		// },
		{
			label: t("kevin_ui.from"),
			field: "deathFrom",
			component: "Select",
			componentProps: {
				options: [
					{
						label: "1 - Emergency Department",
						value: "1"
					},
					{
						label: "2- Examination Department",
						value: "2"
					},
					{
						label: "3 - Treatment Department",
						value: "3"
					}
				]
			}
		},
		{
			label: t("kevin_ui.treatment"),
			component: "Select",
			field: "trtmtPatType",
			componentProps: {
				options: getDictOptions(DICT_TYPE.PATIENT_TYPE)
			}
		},
		// {
		// 	label: t("kevin_ui.referral_from"),
		// 	component: "Input",
		// 	field: "referralFrom",
		// 	colProps: { span: 8 }
		// },
		{
			label: t("kevin_ui.referral_from"),
			component: "Select",
			field: "referralFrom", //谁推荐到类型
			componentProps: {
				options: [
					{ label: "1 health agency", value: "1" },
					{ label: "2 Non-referred", value: "2" },
					{ label: "3 Orther", value: "3" }
				]
			}
		},
		{
			label: t("kevin_ui.name"),
			component: "Input",
			field: "referralName" //推荐场所名称
		},
		{
			label: t("kevin_ui.treatment_start_date"),
			component: "DatePicker",
			field: "trtmtStartDtime" //治疗开始时间
		},
		{
			label: t("kevin_ui.department"),
			component: "Input",
			field: "deptId" //部门
		},
		{
			label: t("kevin_ui.document"),
			component: "Select",
			field: "insDocumentType",
			componentProps: {
				options: [
					//携带医保所需证件类型
					{ label: "1. With referral letter", value: "1" },
					{ label: "2. Without referral letter", value: "2" },
					{ label: "3. With birth certificate", value: "3" },
					{ label: "4. With proof of birth", value: "4" },
					{ label: "5. With discharge certificate", value: "5" },
					{ label: "6. With work permit", value: "6" }
				]
			}
		}
	];
	const formRightBottomFormSchema: FormSchema[] = [
		{
			label: t("kevin_ui.discharge_date"),
			component: "DatePicker",
			field: "dcDtime" //出院时间
		},
		{
			label: t("kevin_ui.length_of_stay"),
			component: "InputNumber",
			field: "trtmtDays" //住院天数
		},
		{
			label: t("kevin_ui.examination"),
			field: "dcFacType", //出院地点类型，门诊/住院
			component: "Select",
			componentProps: {
				options: [
					{ label: "1 Outpatient", value: "1" },
					{ label: "2 Inpatient", value: "2" }
				]
			}
		},

		{
			label: t("kevin_ui.condition"),
			field: "dcType", //出院类型
			component: "Select",
			componentProps: {
				showSearch: true,
				onSearch: value => {
					updateSchema([
						{
							field: "trtmtPatType",
							componentProps: {
								options: [
									{ label: "1 ", value: "1 " },
									{ label: "2  ", value: "2 " }
								]
							}
						}
					]);
				},
				options: [
					{ label: "1.Discharge", value: "1" },
					{ label: "2. Severe illness, request to go home", value: "2" },
					{ label: "3. Leave without notice", value: "3" },
					{ label: "4. Escape from hospital", value: "4" },
					{ label: "5. Transfer to another hospital", value: "5" },
					{ label: "6. Death", value: "6" },
					{ label: "7. Minor illness, request to go home", value: "7" },
					{ label: "8. Death within 6 hours", value: "8" },
					{ label: "9. Death (pregnancy, childbirth)", value: "9" },
					{ label: "10. Refuse to be admitted", value: "10" },
					{ label: "11. Admit", value: "11" }
				]
			}
		},
		{
			label: t("kevin_ui.record_number"),
			field: "dcRxId", //诊断编号
			component: "Input",
			componentProps: {},
			colProps: {
				span: 24
			}
		},

		{
			label: t("dsg_notes"),
			component: "Select",
			field: "isRx", //是否复诊
			componentProps: {
				options: [
					{ label: "yes", value: true },
					{ label: "no", value: false }
				]
			}
		},
		{
			label: t("kevin_ui.cmplication"),
			component: "Select",
			field: "isDisaster", //是否灾难
			componentProps: {
				options: [
					{ label: "yes", value: true },
					{ label: "no", value: false }
				]
			}
		},
		{
			label: t("kevin_ui.pathology"),
			component: "Select",
			field: "isPathology", //是否解剖
			componentProps: {
				options: [
					{ label: "yes", value: true },
					{ label: "no", value: false }
				]
			}
		},
		{
			label: t("kevin_ui.complications"),
			component: "Select",
			field: "iscomplication", //是否伴随并发症
			componentProps: {
				options: [
					{ label: "yes", value: true },
					{ label: "no", value: false }
				]
			}
		}
	];
	const [formRightTopItems, { validateFields: validateFieldsAdmission, updateSchema }] = useForm({
		labelWidth: 120,
		schemas: formRightTopFormSchema,
		autoSubmitOnEnter: false,
		showActionButtonGroup: false,
		baseColProps: { span: 12 }
	});
	const [formRightBottomItems, { validateFields: validateFieldsDischarge }] = useForm({
		labelWidth: 120,
		schemas: formRightBottomFormSchema,
		autoSubmitOnEnter: false,
		showActionButtonGroup: false,
		baseColProps: { span: 12 }
	});
	const [registerTransferDrawer, { openDrawer: openDrawerTransfer }] = useDrawer();

	const patientInfoRef = useRef({
		patientId: ""
	});
	const rxIdRef = useRef("");

	const schemasStart = [
		{
			label: t("felix_ui.admission_time"),
			field: "dateAdmission"
		},
		{
			label: t("kevin_ui.reasonForAdmission"),
			field: "adReason"
		},
		{
			label: t("felix_ui.patient_ID"),
			field: "patId"
		},
		{
			label: t("kevin_ui.socialInsuranceID"),
			field: "insId"
		},
		{
			label: t("felix_ui.adRecievedFrom"),
			field: "adRecievedFrom"
		},
		{
			label: t("flandre_ui.doctor"),
			field: "atdDoctor"
		},
		{
			label: t("felix_ui.atdNurse"),
			field: "atdNurse"
		},
		{
			label: t("kevin_ui.admissionStaff"),
			field: "adStaffId"
		},
		{
			label: t("kevin_ui.emergencyContact"),
			field: "emgContactName"
		},
		{
			label: t("kevin_ui.emergencyContactPhone"),
			field: "emgContactPhone"
		}
	];
	const schemasEnd = [
		{
			label: t("felix_ui.dcDtime"),
			field: "dcDtime"
		},
		{
			label: t("kevin_ui.dischargeSlipNumber"),
			field: "dcNo"
		},
		{
			label: t("kevin_ui.lengthOfStay"),
			field: "trtmtDays"
		},
		{
			label: t("kevin_ui.reasonForDischarge"),
			field: "dcReason"
		},
		{
			label: t("kevin_ui.complications"),
			field: "isComplication"
		},
		{
			label: t("kevin_ui.autopsy"),
			field: "isPathology"
		},
		{
			label: t("kevin_ui.accident"),
			field: "isDisaster"
		},
		{
			label: t("kevin_ui.followUpVisit"),
			field: "isRx"
		}
	];
	const [register] = useFetchHistoryData({
		api: patientSynthesisList,
		contents: "createdTime",
		id: patientInfoRef.current.patientId,
		contentList: [
			{
				title: t("felix_ui.admission_time"),
				schemas: schemasStart,
				key: "ipmAdmission"
			},
			{
				title: t("felix_ui.dcDtime"),
				schemas: schemasEnd,
				key: "ipmDischarge"
			}
		],
		parameter: {
			pageNo: 1,
			pageSize: 10,
			patientId: patientInfoRef.current.patientId
		}
	});

	/** Function */

	const patientSearchSuccess = (data: any) => {
		patientInfoRef.current = data;
		thIpmAdmissionsFn();
	};

	const handlerToolbarAction = (key: string) => {
		switch (key) {
			case "save":
				createAdmissionInfo();
				break;
			case "save_discharge":
				createDischargeInfo();
				break;
			case "transfer":
				openDrawerTransfer(true, {
					record: patientInfoRef.current
				});
				break;
		}
	};

	/** Request */

	const createAdmissionInfo = async () => {
		try {
			const values = await validateFieldsAdmission();
			await admissionsCreate({
				...values,
				patId: patientInfoRef.current.patientId
				// rxId: "a1a9d531caf90c1a12891b308922d022"
			});
			createMessage.success("Add success");
		} catch (err) {
			createMessage.error("Add fail!");
		}
	};

	const createDischargeInfo = async () => {
		try {
			const formValue = await validateFieldsDischarge();
			await dischargesCreate({
				...formValue,
				adId: patientInfoRef.current.patientId
			});
			createMessage.success("Add success");
		} catch (err) {
			createMessage.error("Add fail!");
		}
	};

	const thIpmAdmissionsFn = async () => {
		const res = await thIpmAdmissions({ patientId: patientInfoRef.current.patientId });
		rxIdRef.current = res;
		console.log("诊断", res);
	};

	return (
		<div className="outpatient page">
			<div className="page-content flex flex-col">
				<div className="block flex-none">
					<PatientInformation successFn={patientSearchSuccess} />
				</div>
				<div className="form mt-2 flex-auto">
					<div className="left_form block">
						<div className="block-label">Ⅱ.{t("kevin_ui.INFORMATION")}</div>
						<BasicForm register={formRightTopItems} labelWrap={true}></BasicForm>
						<div className="block-label">Ⅲ. {t("kevin_ui.discharge_information")}</div>
						<BasicForm register={formRightBottomItems} labelWrap={true}></BasicForm>
					</div>
					<div className="right_form block ml-2">
						<div className="block-label">Ⅳ. {t("kevin_ui.discharge_history")}</div>
						<div className="right_form_content">
							<History register={register}></History>
						</div>
					</div>
				</div>
			</div>
			<Toolbar btnList={btnList} action={handlerToolbarAction} />
			<TransferPatient register={registerTransferDrawer}></TransferPatient>
		</div>
	);
};
export default outpatient;
